const path = require('path');
const webpack = require('webpack');
const HTMLWebPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
    entry: {
        index:'./src/index.ts',
        reader:'./src/reader.ts',
        live:'./src/live.ts'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name]_[hash:8].js',
        //配置打包环境，告诉webpack不使用箭头函数
        environment: {
            arrowFunction: false,
            const: false
        }
    },
    module: {
        rules: [
            {
                test: /\.html?$/ ,
                use: ['html-loader']
            },
            {
                test: /\.ts$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    },
                    'ts-loader'
                ],
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: "postcss-loader",
                        options: {
                            postcssOptions: {
                                plugins: [
                                    [
                                        "postcss-preset-env", {
                                            browsers: 'last 2 versions'
                                        }
                                    ]
                                ]
                            }
                        }
                    }
                ]
            },
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'sass-loader',
                    {
                        loader: "postcss-loader",
                        options: {
                            postcssOptions: {
                                plugins: [
                                    [
                                        "postcss-preset-env", {
                                            browsers: 'last 2 versions'
                                        }
                                    ]
                                ]
                            }
                        }
                    }
                ]
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js']
    },
    mode: 'development',
    plugins: [
        new HTMLWebPlugin({
            filename:"index.html",
            template: "./src/index.html",
            inject: 'head',
            chunks: ['index'],
            inlineSource: '.(js|css)$'
        }),
        new HTMLWebPlugin({
            filename:"reader.html",
            template: "./src/reader.html",
            inject: 'head',
            chunks: ['reader'],
            inlineSource: '.(js|css)$'
        }),
        new HTMLWebPlugin({
            filename:"live.html",
            template: "./src/live.html",
            inject: 'head',
            chunks: ['live'],
            inlineSource: '.(js|css)$'
        }),
        new MiniCssExtractPlugin({
            filename: 'css/[name]_[hash:8].css',
            chunkFilename: '[id].css'
        }),
        new CleanWebpackPlugin()
    ],
    devServer: {
        host: '127.0.0.1',
        port: 8083,
        open: true,
        proxy: {
            '/media':{
                target: 'http://e.dangdang.com',
                changeOrigin: true,
                pathRewrite: {
                    '^/media': '/media'
                },
                secure: false
            }
        }     
        
    }
}